#include "types.h"
#include "user.h"
#include "date.h"

// 判断闰年
int is_leap_year(int year)
{
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// 获取某年某月的天数
int get_max_days(int year, int month)
{
    int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    if (month == 2 && is_leap_year(year))
    {
        return 29;
    }
    return days[month - 1];
}

int main(int argc, char *argv[])
{
    struct rtcdate r;

    if (date(&r))
    {
        printf(2, "date failed\n");
        exit();
    }

    struct
    {
        int year, month, day, hour, minute, second;
    } local;
    local.year = r.year;
    local.month = r.month;
    local.day = r.day;
    local.hour = r.hour;
    local.minute = r.minute;
    local.second = r.second;

    // 调整小时
    local.hour += 8;
    if (local.hour >= 24)
    {
        local.hour -= 24;
        local.day += 1;
    }

    // 处理天数溢出
    int max_days = get_max_days(local.year, local.month);
    if (local.day > max_days)
    {
        local.day = 1;
        local.month += 1;
        if (local.month > 12)
        {
            local.month = 1;
            local.year += 1;
        }
    }

    // 打印东八区时间
    printf(1,"%d年%d月%d日%d:%d:%d\n",
           local.year, local.month, local.day,
           local.hour, local.minute, local.second);

    exit();
}